/*
  
  
  * 
 */
package Clases;

import java.io.IOException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.logging.Level;
import java.util.logging.Logger;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;



/**
 *
 * @author Victor
 */
public class Encriptacion {
     
    public String GenerarMD5(String texto){
        String encriptado="";
        byte[] defaultBytes = texto.getBytes();
        try {
            MessageDigest algorithm = MessageDigest.getInstance("MD5");
            algorithm.reset();
            algorithm.update(defaultBytes);
            byte messageDigest[] = algorithm.digest();                
            StringBuffer hexString = new StringBuffer();
            for (int i=0;i<messageDigest.length;i++) {
                String hex = Integer.toHexString(0xFF & messageDigest[i]);
                if (hex.length() == 1){
                    hexString.append('0');
                }
                hexString.append(hex);
            }            
            encriptado = hexString.toString();
            System.out.println("Texto: "+texto+" md5: "+encriptado+" hash: "+texto.hashCode());            
            texto=hexString+"";
        } catch (NoSuchAlgorithmException ex) {
            Logger.getLogger(Encriptacion.class.getName()).log(Level.SEVERE, null, ex);
        }
        return encriptado;
    }
        
    public String EncryptBase64(String texto){  
        String encode="";
        byte[] hash = texto.getBytes();
        BASE64Encoder b64enc = new BASE64Encoder();
        encode = b64enc.encode(hash);       
        return encode;
    }
    
    public String DecryptBase64(String texto){
        String decode="";
        BASE64Decoder b64enc = new BASE64Decoder();
        try {
            byte[] output = b64enc.decodeBuffer(texto);            
            decode = new String (output,"UTF8");
        } catch (IOException ex) {
            Logger.getLogger(Encriptacion.class.getName()).log(Level.SEVERE, null, ex);
        }
        return  decode;
    }
            
    
    
}
